<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@page import="java.io.*" %>
     <%@page import="mavenWebProject.userMapper" %>
     <%@page import="mavenWebProject.user" %>
     <%@page import="mavenWebProject.productsMapper" %>
     <%@page import="mavenWebProject.products" %>
     <%@page import="mavenWebProject.order" %>
     <%@page import="mavenWebProject.car" %>
     <%@page import="java.util.*" %>
     <%@page import="javax.servlet.*" %>
     <%@page import="org.apache.ibatis.io.Resources" %>
     <%@page import="org.apache.ibatis.session.SqlSession"%>
     <%@page import="org.apache.ibatis.session.SqlSessionFactory" %>
     <%@page import="org.apache.ibatis.session.SqlSessionFactoryBuilder" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员</title>
</head>

<style>
    body{font-family: 楷体;font-size: 18px;}

    table[id="tableGuide"]{
        position: absolute;
        top:0%;
        left:0%;
        width:100%;
        height:200px;
        background-color: black;
        color:white;
    }

    table[id="tableManagePurchaser"]{display:block;position: absolute;top:60px;left:0px;background-color:rgb(241,245,251);width:100%;height:700px;border-top-left-radius: 10px;border-top-right-radius: 10px;}
    
     a:link{color:white;text-decoration:none;padding:10px;}
    a:hover{padding:8px;border-radius: 5px;background-color: aliceblue;color:black;}
    a:visited:hover{color:black;}
    a:visited{color:white;}
    a[id="manageProducts"]{color:white;}
    a[id="manageProducts"]:hover{color:black;}
    a[id="tableProductsManageSearchButton"]{margin-left:20px;padding:8px;background-color:white;border-radius:8px;}

    p{margin:0px;}
</style>

<body>
  	<%
    String username=String.valueOf(session.getAttribute("username"));
    %>
	<table id="tableGuide" border="0" cellspacing="10px">
        <tr></tr>
 		<tr><td width="30px"></td><td width="60px"><a href="managerScene.jsp">首页</a></td><td width="100px"><a id="hrefa" href="managerManageProducts.jsp">商品管理</a></td><td width="100px"><a id="hrefa" href="managerManageOrder.jsp">订单管理</a></td><td width="100px"><a id="hrefa" href="managerShowSold.jsp">销量排行</a></td><td width="100px"><a id="hrefa" href="managerManagePurchaser.jsp">用户情况</a></td><td width="100px"></td><td align="right" width="800px">欢迎您：<font style="font-size:24px;margin:10px;color:cyan;"><%=username%></font>&nbsp;&nbsp;<a style="color:red" onClick="exit()">注销</a></td></tr>	        
 		<tr><td colspan="8" height="150px"></tr>
    </table>

    <table id="tableManagePurchaser" border="0">
    <form id="managePurchase">
        <tr><td width="150px" valign="bottom" height="50px"><font style="color:blue;font-size:22px;font-weight:bolder;margin-top:20px;margin-left:10px;">|用户情况</font></td><td width="1400px">&nbsp;</td></tr>
        <tr><td height="60px">&nbsp;</td><td><input placeholder="输入您想查看的用户或重要性群体" id="managePurchaser" name="managePurchaser" type="text" style="border:1px solid black;width:400px;height:30px;border-radius:5px;background-color:rgb(0,0,0,0);"><a style="margin-left:20px;border-radius:8px;color:white;margin-top:10px;background-color:blueviolet;padding-right:15px;padding-left:15px;padding:8px;" onClick="searchPurchaser()">查找</a>
        <a style="margin-left:20px;border-radius:8px;color:white;margin-top:10px;background-color:blueviolet;padding-right:15px;padding-left:15px;padding:8px;" href="managerManagePurchaser.jsp">重置</a>
        <br>
        </td></tr>
        <tr><td>&nbsp;</td><td>
            <table border="0">
                <tr align="center" height="60px" style="font-size:20px;font-weight:bold;"><td rowspan="999" width="20px"></td><td width="200px">用户名</td><td width="200px">用户重要性</td><td width="200px">账户资金（元）</td><td width="200px">消费笔数（笔）</td><td width="200px">总消费（元）</td><td width="200px">消费倾向</td></tr>
                <tr><td colspan='6'><hr></td></tr>
                <%
                String resource = "mybatis-config.xml";
                request.setCharacterEncoding("utf-8");
            	InputStream inputStream = Resources.getResourceAsStream(resource);
            	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            	SqlSession sqlsession=sqlSessionFactory.openSession();
            	userMapper userMapper=sqlsession.getMapper(userMapper.class);
            	productsMapper productsMapper=sqlsession.getMapper(productsMapper.class);
            	List<user> user=userMapper.selectAllUser();
            	String search=request.getParameter("managePurchaser");
            	
            	String name="";int countClothing=0;int countBag=0;int countJewelry=0;int count=0;int purchase=0;int money=0;String importance="";String tendency="";
            	for(user s:user){
            		name=s.getUsername().trim();money=s.getMoney();//消费判定用户重要性
            		if(!name.contentEquals("系统账户")){
            			if(!name.contentEquals("统计网页访问次数")){
            			List<order> order=productsMapper.selectAllOrder(name);
            			for(order x:order){
            				count+=x.getAmount();purchase+=x.getAmount()*x.getPrice();//拿到消费笔数和总消费判定用户重要性
            				List<products> products=productsMapper.selectByName(x.getName().trim());
            				for(products y:products){//拿到判定消费倾向的数据
            					if(y.getKind().trim().contentEquals("clothing")){
            						countClothing+=x.getAmount();
            					}else if(y.getKind().trim().contentEquals("bag")){
            						countBag+=x.getAmount();
            					}else if(y.getKind().trim().contentEquals("jewelry")){
            						countJewelry+=x.getAmount();
            					}
           					}
            			}
            		if(money>500000 && purchase>100000){
            			importance="<font style='color:red;font-size:24px'>重要关注客户</font>";
            		}else if(money>500000 || purchase>100000){
            			importance="<font style='color:orange;font-size:22px'>重要客户</font>";
            		}else if(money>500000 && purchase>100000){
            			importance="<font style='color:green;font-size:20px'>潜力客户</font>";
            		}else if(money>250000 && purchase>50000){
            			importance="<font style='color:cyan;font-size:20px'>一般重要客户</font>";
            		}else{
            			importance="<font style='color:black;font-size:20px'>普通客户</font>";
            		}
            		if(countClothing!=0 || countBag!=0 || countJewelry!=0){
            		if(countClothing>countBag){
            			if(countClothing>countJewelry){tendency="服装";}else if(countClothing==countJewelry){tendency="服装<br>珠宝";}else if(countClothing<countJewelry){tendency="珠宝";}
            		}else if(countClothing==countBag){
            			if(countClothing>countJewelry){tendency="服装<br>背包";}else if(countClothing==countJewelry){tendency="服装<br>珠宝<br>背包";}else if(countClothing<countJewelry){tendency="珠宝";}
            		}else if(countClothing<countBag){
            			if(countBag>countJewelry){tendency="背包";}else if(countBag==countJewelry){tendency="背包<br>珠宝";}else if(countBag<countJewelry){tendency="珠宝";}
            		}
            		}else{
            			tendency="该用户暂未消费";
            		}
            		if(search!=null){
            			if(name.contains(search) || importance.contains(search)){
            				if(importance.contentEquals("<font style='color:red;font-size:24px'>重要关注客户</font>")){
                        		out.println("<tr align='center' style='font-size:22px;color:red;'><td>"+name+"</td><td>"+importance+"</td><td>￥"+money+"</td><td>"+count+"</td><td>￥"+purchase+"</td><td>"+tendency+"</td></tr><tr><td colspan='7'><hr></td></tr>");
            				}else{
                        		out.println("<tr align='center'><td>"+name+"</td><td>"+importance+"</td><td>￥"+money+"</td><td>"+count+"</td><td>￥"+purchase+"</td><td>"+tendency+"</td></tr><tr><td colspan='7'><hr></td></tr>");
            				}
            			}
            		}else{
            			if(importance.contentEquals("<font style='color:red;font-size:24px'>重要关注客户</font>")){
                    		out.println("<tr align='center' style='font-size:22px;color:red;'><td>"+name+"</td><td>"+importance+"</td><td>￥"+money+"</td><td>"+count+"</td><td>￥"+purchase+"</td><td>"+tendency+"</td></tr><tr><td colspan='7'><hr></td></tr>");
        				}else{
                    		out.println("<tr align='center'><td>"+name+"</td><td>"+importance+"</td><td>￥"+money+"</td><td>"+count+"</td><td>￥"+purchase+"</td><td>"+tendency+"</td></tr><tr><td colspan='7'><hr></td></tr>");
        				}
            		}
            		countClothing=0;countBag=0;countJewelry=0;count=0;importance="";purchase=0;
            		}
            	}}
                %>
            </table>    
        </td></tr>
    </form>
    </table>
    <script>
    	function searchPurchaser(){
    		document.getElementById("managePurchase").method="post";document.getElementById("managePurchase").action="managerManagePurchaser.jsp?search=1";document.getElementById("managePurchase").submit();
    	}
    </script>
    


</body>
</html>